﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace WindowsFormsApplication1
{
    class StockData
    {
        public String Code; // 股票编码
        public String Name; // 股票名称
        public String Date; // 交易日期
        public double Open = 0.0; // 开盘价
        public double High = 0.0; // 最高价
        public double Low = 0.0; // 最低价
        public double Close = 0.0; // 最后一次交易价格，相当于收盘价
        public double Volume = 0.0;// 总交易手
        public double Adj = 0.0; // 最后一次交易价格 (今天的收盘价当做加权价格)
    }

     class GetDataFromYahooUtil
    {
         public static String YAHOO_FINANCE_URL = "http://table.yahoo.com/table.csv?";
        public static   String YAHOO_FINANCE_URL_TODAY = "http://ichart.yahoo.com/table.csv?";


        public static  List<StockData> GetStockCsvData(String stockName, String fromDate, String toDate)
        {
             List<StockData> list = new List<StockData>();
            String[] datefromInfo= fromDate.Split('/');
            String[] toDateInfo = toDate.Split('/');
            String code = stockName.Substring(0, 6);
        
            String a = (int.Parse(datefromInfo[1])-1)+"";// a – 起始时间，月
            String b = datefromInfo[2];// b – 起始时间，日
            String c =  datefromInfo[0];// c – 起始时间，年
            String d = (int.Parse(toDateInfo[1])-1)+"";// d – 结束时间，月
            String e = toDateInfo[2];// e – 结束时间，日
            String f =  toDateInfo[0];// f – 结束时间，年
        
            String param = "&a=" + a + "&b=" + b + "&c=" + c + "&d=" + d + "&e="
                    + e + "&f=" + f;
            String url = YAHOO_FINANCE_URL + "s=" + stockName + param;



            WebRequest wr ;
            WebResponse wrs;
            Stream ins;
            StreamReader inputString=null;

            try
            {
                wr = WebRequest.Create(url);
                wrs = wr.GetResponse();

                ins = wrs.GetResponseStream();
                inputString = new StreamReader(ins, Encoding.UTF8);

                String newLine = inputString.ReadLine();

                while ((newLine = inputString.ReadLine()) != null)
                {
                    String[] stockInfo = newLine.Trim().Split(',');
                    StockData sd = new StockData();
                    sd.Code = code;
                    sd.Date = stockInfo[0];
                    sd.Open = double.Parse(stockInfo[1]);
                    sd.High = double.Parse(stockInfo[2]);
                    sd.Low = double.Parse(stockInfo[3]);
                    sd.Close = double.Parse(stockInfo[4]);
                    sd.Volume = double.Parse(stockInfo[5]);
                    sd.Adj = double.Parse(stockInfo[6]);

                    list.Add(sd);
                }


            }
            catch (IOException ex)
            {
                return null;
            }
            finally
            {
                if (inputString != null)
                {
                    
                        inputString.Close();
        
                }
            }

            






            return list;
        }
        


        /**
         * 根据 股票编码、日期 获取股票数据
         * @author 祁丛生
         * @param stockName   沪市：000000.ss 深市：000000.sz
         * @param date 日期
         * @return StockData
         */
        public static  StockData GetStockCsvData(String stockName, String date){
            List<StockData> list = GetStockCsvData(stockName,date,date);
            return ((list.Count>0)?list[0]:null);
        }
        /**
         * 根据 股票编码 获取当天股票数据
         * @author 祁丛生
         * @param stockName   沪市：000000.ss 深市：000000.sz
         * @return StockData
         */
        public static  StockData GetStockCsvData(String stockName){
            String date = DateTime.Now.ToShortDateString().ToString();
            List<StockData> list = GetStockCsvData(stockName,date,date);
            return ((list!=null&&list.Count>0)?list[0]:null);
        }
    



    }
}
